package com.wzx.user.utils;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolUtil {
    private static ThreadPoolExecutor threadPool;

    public static void init(int corePoolSize, int maximumPoolSize, long keepAliveTime, int queueSize) {
        if (threadPool == null) {
            synchronized (ThreadPoolUtil.class) {
                if (threadPool == null) {
                    threadPool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.MILLISECONDS,
                            new ArrayBlockingQueue<>(queueSize), new ThreadPoolExecutor.CallerRunsPolicy());
                }
            }
        }
    }

    public static ThreadPoolExecutor getThreadPool() {
        if (threadPool == null) {
            throw new IllegalStateException("ThreadPoolUtil is not initialized. Call init() first.");
        }
        return threadPool;
    }
}